import axios from '@/utils/axios'
import type {
  LoginRequest,
  LoginResult,
  RegisterRequest,
  UserVO,
  UserInfoUpdateRequest,
  Result,
} from '@/types'

export const authApi = {
  async register(data: RegisterRequest) {
    const response = await axios.post<Result<LoginResult>>('/user/register', data)
    return response.data
  },

  async login(data: LoginRequest) {
    const response = await axios.post<Result<LoginResult>>('/user/login', data)
    return response.data
  },
  
  async refreshToken() {
    const response = await axios.get<Result<LoginResult>>('/user/login/refresh')
    return response.data
  },

  async getCurrentUser() {
    const response = await axios.get<Result<UserVO>>('/user/login/userInfo')
    return response.data
  },

  async logout() {
    const response = await axios.get<Result<boolean>>('/user/logout')
    return response.data
  },

  async checkUserName(userName: string) {
    const response = await axios.get<Result<boolean>>('/user/register/checkName', {
      params: { userName },
    })
    return response.data
  },

  async updateAvatar(avatar: string) {
    const response = await axios.get<Result<boolean>>('/user/avatar/update', {
      params: { avatar },
    })
    return response.data
  },

  async updateUserInfo(data: UserInfoUpdateRequest): Promise<Result<boolean>> {
    const response = await axios.post<Result<boolean>>('/user/userInfo/update', data)
    return response.data
  },
}
